package com.wbs.shop.nuby.utils;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

/**
 * Created by zhoucl on 2017/12/6 0006.
 */

public class ReflectUtil {
    public static <T> T get(Object target, String methodName, Class<T> returnType, Class<?>[] parameterTypes, Object[] parameter) {
        if (target != null) {
            try {
                Method method = target.getClass().getDeclaredMethod(methodName, parameterTypes);
                method.setAccessible(true);
                T value = (T) method.invoke(target, parameter);
                method.setAccessible(false);
                return value;
            } catch (Exception e) {
                Logger.e(e);
                return null;
            }
        }
        return null;
    }

    public static <T> T getFiledValue(Object src, String filedName) {
        try {
            Field declaredField = src.getClass().getDeclaredField(filedName);
            if (Modifier.isPrivate(declaredField.getModifiers())) {
                declaredField.setAccessible(true);
            }
            T t = (T) declaredField.get(src);
            if (Modifier.isPrivate(declaredField.getModifiers())) {
                declaredField.setAccessible(false);
            }
            return t;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
